package person.aoao.library.service.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import person.aoao.library.dao.FloorListDao;
import person.aoao.library.dao.SeatListDao;
import person.aoao.library.dao.TypeListDao;
import person.aoao.library.entity.FloorList;
import person.aoao.library.entity.TypeList;
import person.aoao.library.service.FloorListService;

import javax.annotation.Resource;
import java.util.List;

/**
 * (FloorList)表服务实现类
 *
 * @author makejava
 * @since 2023-05-05 10:15:41
 */
@Service("floorListService")

public class FloorListServiceImpl implements FloorListService {
    @Resource
    private FloorListDao floorListDao;
    @Resource
    private TypeListDao typeListDao;

    @Resource
    private SeatListDao seatListDao;
    /**
     * 通过ID查询单条数据
     *
     * @param fid 主键
     * @return 实例对象
     */
    @Override
    public FloorList queryById(Integer fid) {
        return this.floorListDao.queryById(fid);
    }

    /**
     * 分页查询
     *
     * @param floorList   筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<FloorList> queryByPage(FloorList floorList, PageRequest pageRequest) {
        long total = this.floorListDao.count(floorList);
        return new PageImpl<>(this.floorListDao.queryAllByLimit(floorList, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param floorList 实例对象
     * @return 实例对象
     */
    @Override
    public FloorList insert(FloorList floorList) {
        this.floorListDao.insert(floorList);
        TypeList type=new TypeList();
        type.setFid(floorList.getFid());
        for(Integer typeid:floorList.getTypeids()){
            type.setTypeid(typeid);
            this.typeListDao.update(type);
        }
        floorList.setType(this.typeListDao.getFloorType(floorList.getFid()));
        return floorList;
    }

    /**
     * 修改数据
     *
     * @param floorList 实例对象
     * @return 实例对象
     */
    @Override
    public FloorList update(FloorList floorList) {
        this.floorListDao.update(floorList);
        TypeList type=new TypeList();
        type.setFid(floorList.getFid());
        this.typeListDao.deletefid(floorList.getFid());
        for(Integer typeid:floorList.getTypeids()){
            type.setTypeid(typeid);
            this.typeListDao.update(type);
        }
        floorList.setType(this.typeListDao.getFloorType(floorList.getFid()));
        return floorList;
    }

    /**
     * 通过主键删除数据
     *
     * @param fid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer fid) {
        return this.floorListDao.deleteById(fid) > 0;
    }

    @Override
    public List<FloorList> getFloorList() {
        List<FloorList> list=this.floorListDao.getFloorList();
        for(FloorList item:list){
            item.setSeatCount(this.seatListDao.getSeatList(item.getFid()).size());
            item.setType(this.typeListDao.getFloorType(item.getFid()));
        }
        return list;
    }


}
